package com.kingsoft.miot.bridge.config;

import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.TimeUnit;

import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * 定时任务配置
 * 
 * @author HUJIANQING
 *
 */
@Configuration
public class ScheduledConfig implements SchedulingConfigurer {

	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		// TODO Auto-generated method stub

	}

	// 自定义设置线程池
	@Bean(destroyMethod = "shutdown")
	public Executor setTaskExecutor() {
		return new ThreadPoolExecutor(10, 100, 120, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(16),
				// 异常处理
				new RejectedExecutionHandler() {
					@Override
					public void rejectedExecution(Runnable r, java.util.concurrent.ThreadPoolExecutor executor) {
						// 单纯的删除操作
						executor.remove(r);
					}
				});
	}

}
